package com.godme.rabbitmq.repo;

import com.godme.rabbitmq.bean.Order;
import com.godme.rabbitmq.utils.SequenceGenerator;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Component
public class OrderRepository {

    private static final Map<String, Order> orders = new ConcurrentHashMap<>();

    public void insertOrder(Order order){
        String id;
        do{
            id = SequenceGenerator.generateSequence();
        }while (orders.containsKey(id));
        order.setId(id);
        orders.put(id, order);
    }

    public Order findById(String id){
        if(orders.containsKey(id)){
            return orders.get(id);
        }
        return null;
    }


    public List<Order> findAll(){
        return new ArrayList<>(orders.values());
    }

    public void update(Order order){
        orders.put(order.getId(), order);
    }
}
